package com.bibimaya.auto.mapper.dao.utils;

/**
 * TableUtil
 * Created by lijianwei
 */
public class TableUtil {


    public static String classToTableName(String className){
        return classToTableName(className, null);
    }

    /**
     * class name convert to table name
     * @param className
     * @return
     */
    public static String classToTableName(String className, String classNameSuffix){
        if(className == null || className.length() == 0 || className.trim().length() == 0){
            return null;
        }
        if(classNameSuffix != null && !classNameSuffix.isEmpty()){
            className = className.substring(0, className.lastIndexOf(classNameSuffix));
        }
        StringBuffer sb = new StringBuffer();
        sb.append(Character.toLowerCase(className.charAt(0)));
        for(int i = 1; i < className.length(); i++){
            char c = className.charAt(i);
            if(Character.isUpperCase(c)){
                sb.append("_");
            }
            sb.append(Character.toLowerCase(c));
        }
        return sb.toString();
    }

    /**
     * field name convert to column name
     * @param fieldName
     * @return
     */
    public static String fieldToColumnName(String fieldName){
        if(fieldName == null || fieldName.length() == 0 || fieldName.trim().length() == 0){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        sb.append(Character.toLowerCase(fieldName.charAt(0)));
        for(int i = 1; i < fieldName.length(); i++){
            char c = fieldName.charAt(i);
            if(Character.isUpperCase(c)){
                sb.append("_");
            }
            sb.append(Character.toLowerCase(c));
        }
        return sb.toString();
    }

}
